package lib.gui;

import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.DefaultButtonModel;
import javax.swing.JCheckBox;
import javax.swing.JPanel;

import lib.Base.Settings;
import lib.Common.Cast;
import lib.Common.Path;
import lib.XML.XMLParser;
import lib.XML.XMLWriter;


import org.swixml.SwingEngine;


public class GuiSettings {
	/**
	 * Class which represents the settings page
	 */
	private JPanel settingsPanel;
	private Settings settings;
	
	private ButtonGroup chk_profile_guestbook;
	private ButtonGroup chk_profile_visible;
	private ButtonGroup chk_profile_searchable;
	private ButtonGroup chk_profile_silent;
	private ButtonGroup chk_profile_friendlist;
	private ButtonGroup chk_msg_allow;
	private ButtonGroup chk_users_allow_requests;
	private ButtonGroup chk_data_personal;
	private ButtonGroup chk_data_career;
	private ButtonGroup chk_data_hobby;
	private ButtonGroup chk_liveticker_send;
	private ButtonGroup chk_groups_allow_request;
	
	public GuiSettings(JPanel settingsPanel, SwingEngine engine)
	{
		this.chk_profile_guestbook = ((DefaultButtonModel)((JCheckBox)engine.find("guestbook_checkAll")).getModel()).getGroup();
		this.chk_profile_visible = ((DefaultButtonModel)((JCheckBox)engine.find("visible_checkAll")).getModel()).getGroup();
		this.chk_profile_searchable = ((DefaultButtonModel)((JCheckBox)engine.find("searchable_checkY")).getModel()).getGroup();
		this.chk_profile_silent = ((DefaultButtonModel)((JCheckBox)engine.find("silent_checkY")).getModel()).getGroup();
		this.chk_profile_friendlist = ((DefaultButtonModel)((JCheckBox)engine.find("friendlist_checkAll")).getModel()).getGroup();
		this.chk_msg_allow = ((DefaultButtonModel)((JCheckBox)engine.find("msg_allow_checkAll")).getModel()).getGroup();
		this.chk_users_allow_requests = ((DefaultButtonModel)((JCheckBox)engine.find("allow_requests_checkY")).getModel()).getGroup();
		this.chk_data_personal = ((DefaultButtonModel)((JCheckBox)engine.find("personal_checkAll")).getModel()).getGroup();
		this.chk_data_career = ((DefaultButtonModel)((JCheckBox)engine.find("career_checkAll")).getModel()).getGroup();
		this.chk_data_hobby = ((DefaultButtonModel)((JCheckBox)engine.find("hobby_checkAll")).getModel()).getGroup();
		this.chk_liveticker_send = ((DefaultButtonModel)((JCheckBox)engine.find("send_checkY")).getModel()).getGroup();
		this.chk_groups_allow_request = ((DefaultButtonModel)((JCheckBox)engine.find("allow_grp_request_checkY")).getModel()).getGroup();
		this.settingsPanel = settingsPanel;
		
		init();
		initActionListener();
	}
	
	void init()
	{
		XMLParser parser = null;
		
		try {
			
			settings = lib.Com.Container.settings;
			setSelection(String.valueOf(settings.getPGuestbook()), chk_profile_guestbook.getElements());
			setSelection(String.valueOf(settings.getPVisible()), chk_profile_visible.getElements());
			setSelection(String.valueOf(settings.getPSearchable()), chk_profile_searchable.getElements());
			setSelection(String.valueOf(settings.getPSilent()), chk_profile_silent.getElements());
			setSelection(String.valueOf(settings.getPFriendlist()), chk_profile_friendlist.getElements());
			setSelection(String.valueOf(settings.getCAllowed()), chk_msg_allow.getElements());
			setSelection(String.valueOf(settings.getUAllowedFInvites()), chk_users_allow_requests.getElements());
			setSelection(String.valueOf(settings.getDPersonalData()), chk_data_personal.getElements());
			setSelection(String.valueOf(settings.getDCareerData()), chk_data_career.getElements());
			setSelection(String.valueOf(settings.getDHobbys()), chk_data_hobby.getElements());
			setSelection(String.valueOf(settings.getLTSend()), chk_liveticker_send.getElements());
			setSelection(String.valueOf(settings.getGAllowGInvites()), chk_groups_allow_request.getElements());		
			
        } catch (Exception e) {
        	
            e.printStackTrace();
        }finally{
        	parser = null;
        }
	}
	
	private void setSelection(String settingsVal, Enumeration<AbstractButton> chkBoxes)
	{
		while (chkBoxes.hasMoreElements()) {
			
			AbstractButton btn_chk = chkBoxes.nextElement();
			
			if(btn_chk.getName().equals(settingsVal)){
				btn_chk.setSelected(true);
				break;
			}else if(btn_chk.getName().equals(settingsVal)){
				btn_chk.setSelected(true);
				break;
			}else if(btn_chk.getName().equals(settingsVal)){
				btn_chk.setSelected(true);
				break;
			}else if(btn_chk.getName().equals(settingsVal)){
				btn_chk.setSelected(true);
				break;
			}else if(btn_chk.getName().equals(settingsVal)){
				btn_chk.setSelected(true);
				break;
			}
	    }
	}
	
	void initActionListener()
	{
		List<Component> components = getAllPanelComponents(settingsPanel);
		
		ActionListener actionListener = new ActionListener() {
	      public void actionPerformed(ActionEvent actionEvent) {
	        AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
	        
	        String panelName = abstractButton.getParent().getName();
	        String selected = abstractButton.getName();
	        
	        if (panelName.equals("visible")) {
				settings.setPVisible(Integer.parseInt(selected));
			}else if (panelName.equals("guestbook")) {
				settings.setPGuestbook(Integer.parseInt(selected));
			}else if (panelName.equals("searchable")) {
				settings.setPSearchable(Cast.StringToBoolean(selected));
			}else if (panelName.equals("silent")) {
				settings.setPSilent(Cast.StringToBoolean(selected));
			}else if (panelName.equals("visibleFriendlist")) {
				settings.setPFriendlist(Integer.parseInt(selected));
			}else if (panelName.equals("allowChat")) {
				settings.setCAllowed(Integer.parseInt(selected));
			}else if (panelName.equals("allowFinvite")) {
				settings.setUAllowedFInvites(Cast.StringToBoolean(selected));
			}else if (panelName.equals("personalData")) {
				settings.setDPersonalData(Integer.parseInt(selected));
			}else if (panelName.equals("careerData")) {
				settings.setDCareerData(Integer.parseInt(selected));
			}else if (panelName.equals("hobbys")) {
				settings.setDHobbys(Integer.parseInt(selected));
			}else if (panelName.equals("allowGInvites")) {
				settings.setGAllowGInvites(Cast.StringToBoolean(selected));
			}else if (panelName.equals("send")) {
				settings.setLTSend(Cast.StringToBoolean(selected));
			}
	        lib.Com.Container.settings = settings;
	      }
	    };
		
		for(Component comp:components)
		{
			if(comp instanceof JCheckBox){
				
				JCheckBox tmpBox = (JCheckBox)comp;
				tmpBox.addActionListener(actionListener);
			}
		}
	}
	
	private List<Component> getAllPanelComponents(Container c) {
	    Component[] comps = c.getComponents();
	    List<Component> compList = new ArrayList<Component>();
	    
	    for (Component comp : comps) {
	        compList.add(comp);
	        
	        if (comp instanceof Container)
	        {
	            compList.addAll(getAllPanelComponents((Container) comp));
	        }
	    }
	    
	    return compList;
	}
	
	protected void writeSettings(){
		
		XMLWriter writer = new XMLWriter();
		
	    try{
	    	
	    	writer.WriteSettings(settings, Path.XML_PATH_MAIN+"settings.xml");
	    }  catch(Exception e){
	    	e.printStackTrace();
	    }finally{
	    	
	    	writer = null;
	    }
	}
}